//
//  ITSGetImageOperation.m
//  XCar
//
//  Created by Rainbow on 2/14/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "ITSGetImageOperation.h"
#import "UtilHelper.h"

@implementation ITSGetImageOperation
@synthesize _imageView, _imageUrl, _imageName;

-(id)initWithImageView:(ITSImageView *)imageView imageUrl:(NSString *)url nameToSave:(NSString *)name
{
    if ((self = [super init])) {
		self._imageUrl = url;
		self._imageName = name;
		self._imageView = imageView;
	}
	return self;
}

-(id)initWithImageView:(ITSImageView *)imageView imageUrl:(NSString *)url
{
    if ((self = [super init])) {
		self._imageUrl = url;
		self._imageView = imageView;
        self._imageName = nil;
	}
	return self;
}


-(void)main
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if (self._imageUrl == nil||[@"" isEqualToString:self._imageUrl]) {
        [self._imageView performSelectorOnMainThread:@selector(displayImage:) withObject:nil waitUntilDone:YES];
    }
    else
    {
        UIImage *image = [UtilHelper getImageFromUrl:self._imageUrl];
        if (image && _imageName) {
            [UtilHelper storeImageInDocument:image ImageName:self._imageName];
        }
  
        [self._imageView performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:YES];
    }
    
	[pool release];
}

-(void)dealloc
{
    _imageView = nil;
    _imageUrl = nil;
    _imageName = nil;
	[super dealloc];

    
}

@end